import { getBaseUrl, requestUtil } from '../../utils/requestUtil.js';
Page({
    data: {
        orderId: '',
        goodsList: [],
        uploadedImagesMap: {}
    },
    onLoad(options) {
        this.setData({
            orderId: options.orderId,
            goodsList: JSON.parse(options.goodsList)
        });
        this.filterEvaluatedGoods();
    },
    filterEvaluatedGoods() {
        const { orderId, goodsList } = this.data;
        wx.request({
            url: getBaseUrl() + '/my/order/getEvaluatedGoods', // 假设新增一个接口来获取已评价商品列表
            method: 'POST',
            data: {
                orderId: orderId
            },
            success: (res) => {
                if (res.data.code === 0) {
                    const evaluatedGoods = res.data.evaluatedGoods;
                    const newGoodsList = goodsList.filter(goods => {
                        return!evaluatedGoods.some(evalGoods => evalGoods.productId === goods.productId);
                    });
                    this.setData({
                        goodsList: newGoodsList
                    });
                } else {
                    wx.showToast({
                        title: '获取已评价商品列表失败',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                });
                console.error('获取已评价商品列表请求失败:', err);
            }
        });
    },
    chooseImage(e) {
        const productId = e.currentTarget.dataset.productId;
        wx.chooseImage({
            count: 9,
            success: (res) => {
                const uploadedImagesMap = { ...this.data.uploadedImagesMap };
                uploadedImagesMap[productId] = res.tempFilePaths;
                res.tempFilePaths.forEach((imagePath) => {
                    wx.getImageInfo({
                        src: imagePath,
                        success: (infoRes) => {
                            console.log('Image info:', infoRes);
                        },
                        fail: (infoErr) => {
                            console.error('Failed to get image info:', infoErr);
                        }
                    });
                });
                this.setData({
                    uploadedImagesMap
                }, () => {
                    console.log('uploadedImagesMap updated:', this.data.uploadedImagesMap);
                });
            }
        });
    },
    submitEvaluation(e) {
        const productId = e.currentTarget.dataset.productId;
        const orderId = this.data.orderId;
        const score = e.detail.value.score;
        const comment = e.detail.value.comment;
        const uploadedImages = this.data.uploadedImagesMap[productId] || [];
        let uploadedCount = 0;
        const imageUrls = [];

        if (uploadedImages.length === 0) {
            this.submitEvaluationWithImages(orderId, productId, score, comment, imageUrls);
            return;
        }

        uploadedImages.forEach((imagePath, index) => {
            wx.uploadFile({
                url: getBaseUrl() + '/my/order/uploadImages',
                filePath: imagePath,
                name: 'files',
                success: (res) => {
                    const data = JSON.parse(res.data);
                    if (data.code === 0) {
                        imageUrls.push(data.data[0]);
                        uploadedCount++;
                        if (uploadedCount === uploadedImages.length) {
                            this.submitEvaluationWithImages(orderId, productId, score, comment, imageUrls);
                        }
                    } else {
                        wx.showToast({
                            title: '图片上传失败',
                            icon: 'none'
                        });
                    }
                },
                fail: (err) => {
                    wx.showToast({
                        title: '图片上传请求失败',
                        icon: 'none'
                    });
                    console.error('图片上传请求失败:', err);
                }
            });
        });
    },
    submitEvaluationWithImages(orderId, productId, score, comment, imageUrls) {
        wx.request({
            url: getBaseUrl() + '/my/order/submitEvaluation',
            method: 'POST',
            data: {
                orderId: orderId,
                productId: productId,
                score: score,
                comment: comment,
                imageUrls: imageUrls.join(',')
            },
            success: (res) => {
                if (res.data.code === 0) {
                    wx.showToast({
                        title: '评价提交成功',
                        icon:'success'
                    });
                    // 移除已评价商品
                    const goodsList = this.data.goodsList.filter(item => item.productId!== productId);
                    this.setData({
                        goodsList
                    });
                    // 判断是否已经评价完了所有商品
                    if (goodsList.length === 0) {
                        // 跳转到订单评论页，假设评论页路径为 /pages/orderComment/orderComment
                        wx.redirectTo({
                            url: '/pages/order/index'
                        });
                    }
                } else {
                    wx.showToast({
                        title: '评价提交失败',
                        icon: 'none'
                    });
                }
            },
            fail: (err) => {
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                });
                console.error('评价提交请求失败:', err);
            }
        });
    }
});